﻿using System;
using System.Collections.Generic;
using BotonJXC.Entity;
using NHibernate.Criterion;
using BotonJXC.Util;

namespace BotonJXC.DAO
{
    public class ProductDAO : NHibernateDao<Product, int>
    {
        public PageModel<Product> Find(int typeId, string code, string name, string barcode, string spec, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(code))
            {
                criterions.Add(Restrictions.Like("ProductCode", code, MatchMode.Anywhere));
            }
            if (Utils.IsNotBlank(name))
            {
                criterions.Add(Restrictions.Like("ProductName", name, MatchMode.Anywhere));
            }
            if (Utils.IsNotBlank(barcode))
            {
                criterions.Add(Restrictions.Like("Barcode", barcode, MatchMode.Anywhere));
            }
            if (Utils.IsNotBlank(spec))
            {
                criterions.Add(Restrictions.Like("ProductSpec", spec, MatchMode.Anywhere));
            }
            if (typeId > 0)
            {
                criterions.Add(Restrictions.Eq("Type.TypeId", typeId));
            }
            return base.FindByPage(criterions.ToArray(), null, start, count);
        }

        public bool CheckExists(Product product)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            Junction junction = Restrictions.Conjunction();
            Disjunction disjunction = Restrictions.Disjunction();
            if (Utils.IsNotBlank(product.ProductCode))
            {
                disjunction.Add(Restrictions.Like("ProductCode", product.ProductCode, MatchMode.Anywhere));
            }
            if (Utils.IsNotBlank(product.ProductName))
            {
                disjunction.Add(Restrictions.Like("ProductName", product.ProductName, MatchMode.Anywhere));
            }
            if (Utils.IsNotBlank(product.Barcode))
            {
                disjunction.Add(Restrictions.Like("Barcode", product.Barcode, MatchMode.Anywhere));
            }
            if (product.ProductId > 0)
            {
                junction.Add(Restrictions.Not(Restrictions.Eq("ProductId", product.ProductId)));
            }
            criterions.Add(junction);
            criterions.Add(disjunction);
            return GetTotalCount(criterions.ToArray()) > 0;
        }
        public Product FindProduct(string parameter, string value)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(parameter) && Utils.IsNotBlank(value))
            {
                criterions.Add(Restrictions.Eq(parameter, value));
            }

            return base.FindUnique(criterions.ToArray());
        }

        public Product findproductbycode(string code)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (code != null && !"".Equals(code))
            {
                criterions.Add(Restrictions.Eq("ProductCode", code));
            }
            IList<Product> list = base.Find(criterions.ToArray());
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }

        }
    }
}